package org.jeecg.modules.approve.cancel.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.approve.cancel.entity.BikeApproveCancel;
import org.jeecg.modules.approve.cancel.service.IBikeApproveCancelService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.approve.cancel.vo.BikeApproveCancelRes;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 销户审批
 * @Author: 月月鸟
 * @Date:   2021-09-02
 * @Version: V1.0
 */
@Api(tags="销户审批")
@RestController
@RequestMapping("/cancel/bikeApproveCancel")
@Slf4j
public class BikeApproveCancelController extends JeecgController<BikeApproveCancel, IBikeApproveCancelService> {
	@Autowired
	private IBikeApproveCancelService bikeApproveCancelService;

	@Autowired
	private BaseCommonService baseCommonService;
	/**
	 * 销户审批-审批列表查询
	 *
	 * @param bikeApproveCancelRes
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="销户审批-审批列表查询", notes="销户审批-审批列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikeApproveCancelRes bikeApproveCancelRes,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		return bikeApproveCancelService.pageList(bikeApproveCancelRes,pageNo,pageSize);
	}

	 /**
	  * 用户-审批列表查询
	  *
	  * @param bikeApproveCancelRes
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="用户-审批列表查询", notes="用户-审批列表查询")
	 @GetMapping(value = "/listUser")
	 public Result<?> listUser(BikeApproveCancelRes bikeApproveCancelRes,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 bikeApproveCancelRes.setApplyBy(sysUser.getRealname());
		 return bikeApproveCancelService.pageList(bikeApproveCancelRes,pageNo,pageSize);
	 }
	/**
	 *   添加
	 *
	 * @param bikeApproveCancel
	 * @return
	 */
	@AutoLog(value = "会员用户管理-申请销户")
	@ApiOperation(value="销户审批-添加", notes="销户审批-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeApproveCancel bikeApproveCancel) {
		//1.判断状态是否正常 2. 判断是否重复申请销户 3.退卡后销户
		String yn = bikeApproveCancelService.addCheck(bikeApproveCancel	);
		if (!yn.equals(BikeConstant.y_n_yes)){
			return Result.error(yn);
		}
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//审核状态 0 未审核
		bikeApproveCancel.setApproveStatus("0");
		//添加申请人账号姓名
		bikeApproveCancel.setApplyBy(sysUser.getRealname());
		bikeApproveCancelService.save(bikeApproveCancel);
		return Result.OK("添加成功！");
	}
	 /**
	  *   校验添加销户
	  *
	  * @param bikeApproveCancel
	  * @return
	  */
	 @ApiOperation(value="销户审批-校验添加销户", notes="销户审批-校验添加销户")
	 @PostMapping(value = "/check")
	 public Result<?> check(@RequestBody BikeApproveCancel bikeApproveCancel) {
		 //1.判断状态是否正常 2. 判断是否重复申请销户 3.退卡后销户
		 String yn = bikeApproveCancelService.addCheck(bikeApproveCancel	);
		 if (!yn.equals(BikeConstant.y_n_yes)){
			 return Result.error(yn);
		 }
		 return Result.OK1();
	 }
	/**
	 *  编辑
	 *
	 * @param bikeApproveCancel
	 * @return
	 */
	@AutoLog(value = "销户审批-编辑",operateType = 3)
	@ApiOperation(value="销户审批-编辑", notes="销户审批-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeApproveCancel bikeApproveCancel) {
		bikeApproveCancelService.updateById(bikeApproveCancel);
		return Result.OK("编辑成功!");
	}
	 /**
	  *  审批
	  *
	  * @param bikeApproveCancel
	  * @return
	  */
	 @AutoLog(value = "销户审批-审批",operateType = 3)
	 @ApiOperation(value="销户审批-审批", notes="销户审批-审批")
	 @PutMapping(value = "/approve")
	 public Result<?> approve(@RequestBody BikeApproveCancel bikeApproveCancel) {
		 return bikeApproveCancelService.approve(bikeApproveCancel);
	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "销户审批-通过id删除")
	@ApiOperation(value="销户审批-通过id删除", notes="销户审批-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		bikeApproveCancelService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "销户审批-批量删除")
	@ApiOperation(value="销户审批-批量删除", notes="销户审批-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bikeApproveCancelService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="销户审批-通过id查询", notes="销户审批-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeApproveCancel bikeApproveCancel = bikeApproveCancelService.getById(id);
		if(bikeApproveCancel==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeApproveCancel);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikeApproveCancelRes
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeApproveCancelRes bikeApproveCancelRes) {
        List<BikeApproveCancelRes> bikeApproveCancelResList= bikeApproveCancelService.exportList(bikeApproveCancelRes);
    	return ExcelUtils.exportXls(request,bikeApproveCancelResList,BikeApproveCancelRes.class,"销户审批");
    }
	 /**
	  * User导出excel
	  *
	  * @param request
	  * @param bikeApproveCancelRes
	  */
	 @RequestMapping(value = "/exportXlsUser")
	 public ModelAndView exportXlsUser(HttpServletRequest request, BikeApproveCancelRes bikeApproveCancelRes) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 bikeApproveCancelRes.setApplyBy(sysUser.getRealname());
		 List<BikeApproveCancelRes> bikeApproveCancelResList = bikeApproveCancelService.exportList(bikeApproveCancelRes);
		 return ExcelUtils.exportXls(request, bikeApproveCancelResList, BikeApproveCancelRes.class, "销户审批");
	 }


}
